import '../index.dart';
import 'package:hive/hive.dart';

part 'user.g.dart';

@HiveType(typeId: 0)
class UserEntity extends HiveObject {
  @HiveField(0)
  final String id;

  @HiveField(1)
  final String username;

  @HiveField(2)
  final String password;

  @HiveField(3)
  final String avatar;

  @HiveField(4)
  final List<String> share;

  @HiveField(5)
  final bool isVip;

  @HiveField(6)
  final List<String> like;

  @HiveField(7)
  final List<String> reportedPosts; 
  
  @HiveField(8)
  final List<String> reportedUsers;

  @HiveField(9)
  final List<String> reportedChats;

  UserEntity({
    required this.id,
    required this.username,
    required this.password,
    required this.avatar,
    required this.share,
    required this.isVip,
    required this.like,
    this.reportedPosts = const [],
    this.reportedUsers = const [],
    this.reportedChats = const [],
  });

  factory UserEntity.fromJson(Map<String, dynamic> json) {
    return UserEntity(
      id: json['id'],
      username: json['username'],
      password: json['password'],
      avatar: json['avatar'],
      share: json['share'] as List<String>,
      isVip: json['isVip'] ?? false,
      like: json['like'] as List<String>,
      reportedPosts:
          (json['reportedPosts'] as List<dynamic>?)?.cast<String>() ?? [],
      reportedUsers:
          (json['reportedUsers'] as List<dynamic>?)?.cast<String>() ?? [],
      reportedChats:
          (json['reportedChats'] as List<dynamic>?)?.cast<String>() ?? [],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'username': username,
      'password': password,
      'avatar': avatar,
      'share': share,
      'isVip': isVip,
      'like': like,
      'reportedPosts': reportedPosts,
      'reportedUsers': reportedUsers,
      'reportedChats': reportedChats,
    };
  }

  UserEntity copyWith({
    String? id,
    String? username,
    String? password,
    String? avatar,
    List<String>? share,
    bool? isVip,
    List<String>? like,
    List<String>? reportedPosts,
    List<String>? reportedUsers,
    List<String>? reportedChats,
  }) {
    return UserEntity(
      id: id ?? this.id,
      username: username ?? this.username,
      password: password ?? this.password,
      avatar: avatar ?? this.avatar,
      share: share ?? this.share,
      isVip: isVip ?? this.isVip,
      like: like ?? this.like,
      reportedPosts: reportedPosts ?? this.reportedPosts,
      reportedUsers: reportedUsers ?? this.reportedUsers,
      reportedChats: reportedChats ?? this.reportedChats,
    );
  }

  @override
  String toString() {
    return 'UserEntity(id: $id, username: $username, password: $password, avatar: $avatar, share: $share, isVip: $isVip)';
  }

  // 默认用户
  static final UserEntity kina = UserEntity(
    id: '1',
    username: 'kina',
    password: 'kina123',
    avatar: MyImages.kina,
    share: ['1', '3', '7', '9'],
    isVip: false,
    like: ['1', '2', '8', '9'],
    reportedPosts: [],
    reportedUsers: [],
    reportedChats: [],
  );

  static final UserEntity mark = UserEntity(
    id: '2',
    username: 'mark',
    password: 'mark123',
    avatar: MyImages.mark,
    share: ['2', '4', '6', '8'],
    isVip: false,
    like: ['1', '2', '8', '9', '3', '4', '5', '6', '7', '10'],
    reportedPosts: [],
    reportedUsers: [],
    reportedChats: [],
  );

  static final UserEntity hugh = UserEntity(
    id: '3',
    username: 'hugh',
    password: 'hugh123',
    avatar: MyImages.hugh,
    share: ['5', '10'],
    isVip: false,
    like: ['3', '8', '9'],
    reportedPosts: [],
    reportedUsers: [],
    reportedChats: [],
  );

  // 获取所有默认用户的列表
  static List<UserEntity> get defaultUsers => [kina, mark, hugh];
}
